package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mapper.UserMapper;
import com.woniu.pojo.User;
import com.woniu.service.ICarService;
import com.woniu.service.IUserService;
import com.woniu.vo.CarWithOrderListVo;
import com.woniu.vo.UserQueryVo;
import com.woniu.vo.UserWithCarListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Energy
 * @since 2022-10-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private ICarService carService;

    @Override
    public Page<User> queryPage(UserQueryVo userQueryVo, Long currentPage, Long pageSize) {
        // 设置分页条件
        Page<User> page = new Page<>(currentPage, pageSize);

        // 设置查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (userQueryVo != null) {
            if (!StringUtils.isEmpty(userQueryVo.getNickname())) {
                wrapper.like("nickname", userQueryVo.getNickname());
            }
            if (!StringUtils.isEmpty(userQueryVo.getPhone())) {
                wrapper.like("phone", userQueryVo.getPhone());
            }
            if (userQueryVo.getHasWechat() != null) {
                if (userQueryVo.getHasWechat()) {
                    wrapper.isNotNull("wechat");
                } else {
                    wrapper.isNull("wechat");
                }
            }
            if (!StringUtils.isEmpty(userQueryVo.getStatus())) {
                wrapper.eq("status", userQueryVo.getStatus());
            }
        }

        return userMapper.selectPage(page, wrapper);
    }

    @Override
    public Page<UserWithCarListVo> queryWithCarListWithOrderListPage(UserQueryVo userQueryVo, Long currentPage, Long pageSize) {
        // 设置分页条件
        Page<User> page = new Page<>(currentPage, pageSize);

        // 设置查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (userQueryVo != null) {
            if (!StringUtils.isEmpty(userQueryVo.getNickname())) {
                wrapper.like("nickname", userQueryVo.getNickname());
            }
            if (!StringUtils.isEmpty(userQueryVo.getPhone())) {
                wrapper.like("phone", userQueryVo.getPhone());
            }
            if (userQueryVo.getHasWechat() != null) {
                if (userQueryVo.getHasWechat()) {
                    wrapper.isNotNull("wechat");
                } else {
                    wrapper.isNull("wechat");
                }
            }
            if (!StringUtils.isEmpty(userQueryVo.getStatus())) {
                wrapper.eq("status", userQueryVo.getStatus());
            }
        }
        Page<User> userPage = userMapper.selectPage(page, wrapper);

        // 查询车辆数据保存到用户数据里
        List<UserWithCarListVo> voList = new ArrayList<>();
        for (User user : userPage.getRecords()) {
            UserWithCarListVo userVo = new UserWithCarListVo();
            BeanUtils.copyProperties(user, userVo);

            List<CarWithOrderListVo> carVoList = carService.queryWithOrderListByUserId(user.getId());
            userVo.setCarList(carVoList);

            voList.add(userVo);
        }
        Page<UserWithCarListVo> voPage = new Page<>();
        voPage.setRecords(voList);
        voPage.setTotal(userPage.getTotal());
        return voPage;
    }

    /**
     * 根据用户微信号查询用户数据
     * @param wechat  用户微信号
     * @return user对象
     */
    public User searchByWechat(String wechat) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("wechat", wechat);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 注册，绑定手机号
     * @param id  用户id
     * @param phone 用户手机号
     * @return
     */
    public Boolean insertPhone(long id, String phone) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = userMapper.selectOne(queryWrapper);
        if(user==null){
            Boolean flag=userMapper.updatePhoneById(id,phone);
            return flag;
        }
        return false;
    }
}
